import { _decorator, Component, Label, Animation, Node, Sprite, Vec3 } from 'cc';
import { AppControll, EventName, PopView, signIn_config } from '../manager/AppControll';
import { DataHelper } from '../system/DataHelper';
import { GameData } from '../system/GameData';
import { Loader } from '../tools/LoadManger';
import { SevenSignInItem } from './SevenSignInItem';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
import EvtManger from '../tools/EvtManger';
import { CoinDropType } from '../fight/Drop';
import { GetSkinDebris } from './GetSkinDebris';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

@ccclass('SevenSignIn')
export class SevenSignIn extends Component {
    private mNdRewardList: Node = null;
    private mNdBody: Node = null;
    private mConfigList: signIn_config[] = [];
    private mNdGetBtn: Node = null;
    private mNdGetAgainBtn: Node = null;
    private mNdTomorrowBtn: Node = null;
    private mNdBtnList: Node = null;
    private mIsLock: boolean = false;
    private mCurConfig: signIn_config = null;
    private mCurSign: number = null;
    private mNdSelected: Node = null;

    protected onLoad(): void {
        GameData.isPause = true;
        this.mNdBody = this.node.getChildByName("body");
        this.mNdRewardList = this.mNdBody.getChildByName("sevenDays");
        this.mNdBtnList = this.mNdBody.getChildByName("btnList");
        this.mNdGetBtn = this.mNdBtnList.getChildByName("getBtn");
        this.mNdGetAgainBtn = this.mNdBtnList.getChildByName("getAgainBtn");
        this.mNdTomorrowBtn = this.mNdBtnList.getChildByName("tomorrowBtn");
        this.mNdGetBtn.on(Node.EventType.TOUCH_END, this.OnBtnGet, this);
        this.mNdGetAgainBtn.on(Node.EventType.TOUCH_END, this.OnBtnAgain, this);
        let config = DataHelper.GetSignInConfig();
        for (const key in config) {
            if (Object.prototype.hasOwnProperty.call(config, key)) {
                const element = config[key];
                this.mConfigList.push(element);
            }
        }
        this.InitView();
    }

    start() {

    }

    InitView() {
        this.mCurSign = GameData.GetCurSignIn();
        for (let i = 0; i < this.mNdRewardList.children.length; i++) {
            let node = this.mNdRewardList.children[i];
            let config = this.GetConfigBySignRank(i + 1);
            this.InitSignItem(node, config);
        }
        this.InitButton();
    }

    GetConfigBySignRank(rank: number) {
        for (let i = 0; i < this.mConfigList.length; i++) {
            if (this.mConfigList[i].signRank == rank) {
                return this.mConfigList[i];
            }
        }
        console.error("this.mConfigList Null");
    }

    InitSignItem(node: Node, config: signIn_config) {
        if (!config) return;
        let ndReward = node.getChildByName("reward");
        let ndGun = ndReward.getChildByName("gunDebris");
        let ndGold = ndReward.getChildByName("gold");
        ndReward.getChildByName("Received").active = false;
        if (config.signReward == 3) {
            ndGold.active = true;
            ndGold.getChildByName("goldNum").getComponent(Label).string = config.rewardValue;
            ndGun.active = false;
        } else if (config.signReward == 2 && config.replaceType == 3) {
            let isHaveSkin = false;
            GameData.playerData.skinFragments.forEach(elemt => {
                let skinconfig = DataHelper.GetSkin_configById(config.debrisID);
                if (elemt.skinId == config.debrisID && elemt.count >= skinconfig.unlockNeedDebris) {
                    isHaveSkin = true;
                }
            })
            if (GameData.playerData.skin.indexOf(config.debrisID) == -1 && isHaveSkin == false) {
                ndGun.active = true;
                ndGold.active = false;
                let skinConfig = DataHelper.GetSkin_configById(config.debrisID);
                Loader.LoadGunImage("mainGunSkin/" + skinConfig.mainGunSkinRes, (spf) => {
                    ndGun.getChildByName("reward_month_icon_gold").getComponent(Sprite).spriteFrame = spf;
                });
                ndGun.getChildByName("DebrisNum").getComponent(Label).string = config.rewardValue;
                if (ndGun.getChildByName("GunDebris")) {
                    ndGun.getChildByName("GunDebris").getComponent(Label).string = skinConfig.unlockValue
                }
            } else {
                ndGold.active = true;
                ndGold.getChildByName("goldNum").getComponent(Label).string = config.replaceValue;
                ndGun.active = false;
            }
        }

        if (!GameData.playerData.lastSigninTime) {
            if (config.signRank == 1) {
                ndReward.getChildByName("select").active = true;
                this.mNdSelected = ndReward;
                this.scheduleOnce(() => {
                    node.getComponent(Animation).play();
                })
                this.mCurConfig = config;
                if (GameData.playerData.signInCount == 2) ndReward.getChildByName("Received").active = true;
            }
            return;
        }

        if (this.mCurSign == config.signRank) {
            ndReward.getChildByName("select").active = true;
            this.mNdSelected = ndReward;
            this.scheduleOnce(() => {
                node.getComponent(Animation).play();
            })
            this.mCurConfig = config;
            if (GameData.playerData.signInCount == 2) ndReward.getChildByName("Received").active = true;
        } else {
            ndReward.getChildByName("select").active = false;
        }

        if (this.mCurSign > config.signRank) {
            ndReward.getChildByName("Received").active = true;
        }

    }

    InitButton() {
        switch (GameData.playerData.signInCount) {
            case 0:
                this.mNdGetBtn.active = true;
                this.mNdGetAgainBtn.active = false;
                this.mNdTomorrowBtn.active = false;
                break;
            case 1:
                this.mNdGetBtn.active = false;
                this.mNdGetAgainBtn.active = true;
                this.mNdTomorrowBtn.active = false;
                break;
            case 2:
                this.mNdGetBtn.active = false;
                this.mNdGetAgainBtn.active = false;
                this.mNdTomorrowBtn.active = true;
                break;
        }
    }

    OnBtnGet() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick)
        if (this.mIsLock) return;
        GameData.playerData.signInCount++;
        this.GetReward();
        this.InitView();
    }

    GetReward() {
        if (!this.mCurConfig) {
            PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, nd => {
                nd.getComponent(SystemMessage).InitContent("签到数据异常 请稍后重试")
            })
            return;
        }
        if (this.mCurConfig.signReward == 3) {
            let pos = this.mNdSelected.parent.position.clone().add(this.mNdSelected.parent.parent.position.clone());
            EvtManger.do(EventName.dropPearls, this.mCurConfig.rewardNum, pos);
        } else if (this.mCurConfig.signReward == 2) {
            if (GameData.playerData.skin.indexOf(this.mCurConfig.debrisID) == -1) {
                let skinConfig = DataHelper.GetSkin_configById(this.mCurConfig.debrisID);
                let pos = this.mNdSelected.parent.position.clone().add(this.mNdSelected.parent.parent.position.clone());
                EvtManger.do(EventName.dropSkin, skinConfig, this.mCurConfig.rewardNum, pos);
            } else {
                GameData.AddGold(this.mCurConfig.replaceRewardNum);
                let pos = this.mNdSelected.parent.position.clone().add(this.mNdSelected.parent.parent.position.clone());
                EvtManger.do(EventName.dropGiftGold, pos);
            }
        }
        GameData.playerData.lastSigninTime = Date.now();
        GameData.playerData.curSignIn = this.mCurSign;
        GameData.SavePlayData();
    }

    OnBtnAgain() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick)
        if (this.mIsLock) return;
        this.mIsLock = true;
        this.scheduleOnce(() => {
            this.mIsLock = false;
        }, 0.5)
        AppControll.Platform.ShowRewardVideoAd(this, (bool, str) => {
            this.mIsLock = false;
            if (!bool) {
                PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                    node.getComponent(SystemMessage).InitContent(str);
                })
                return;
            }
            AppControll.Platform.SendBuryPoint(100011);
            GameData.playerData.signInCount++;
            this.GetReward();
            this.InitView();
        })
    }

    OnBtnTomorrow() {

    }

    OnClose() {
        GameData.isPause = false;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick)
        this.node.active = false;
    }
}


